<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>内置排序 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_intrinsic_sorts.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_intrinsic_sorts.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_intrinsic_sorts.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_intrinsic_sorts.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="_sorting_multivalue_buckets.html">多桶排序</a></span>
»
<span class="breadcrumb-node">内置排序</span>
</div>
<div class="navheader">
<span class="prev">
<a href="_sorting_multivalue_buckets.html">« 多桶排序</a>
</span>
<span class="next">
<a href="_sorting_by_a_metric.html">按度量排序 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_intrinsic_sorts"></a>内置排序 (Intrinsic Sorts)<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/300_Aggregations/50_sorting_ordering.asciidoc">edit</a>
</h2>
</div></div></div>
<p>这些排序模式是桶 <em>固有的</em> 能力：它们操作桶生成的数据 ，比如 <code class="literal">doc_count</code> 。
 它们共享相同的语法，但是根据使用的桶的不同会有些细微差别。</p>
<p>让我们做一个 <code class="literal">terms</code> 聚合但是按 <code class="literal">doc_count</code> 值的升序排序：</p>
<div class="pre_wrapper lang-sense">
<pre class="programlisting prettyprint lang-sense">GET /cars/transactions/_search
{
    "size" : 0,
    "aggs" : {
        "colors" : {
            "terms" : {
              "field" : "color",
              "order": {
                "_count" : "asc" <a id="CO199-1"></a><i class="conum" data-value="1"></i>
              }
            }
        }
    }
}</pre>
</div>
<div class="sense_widget" data-snippet="snippets/300_Aggregations/50_sorting_ordering.json"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO199-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>用关键字 <code class="literal">_count</code> ，我们可以按 <code class="literal">doc_count</code> 值的升序排序。</p>
</td>
</tr>
</table>
</div>
<p>我们为聚合引入了一个 <code class="literal">order</code> 对象， 它允许我们可以根据以下几个值中的一个值进行排序：</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
<code class="literal">_count</code>
</span>
</dt>
<dd>
按文档数排序。对 <code class="literal">terms</code> 、 <code class="literal">histogram</code> 、 <code class="literal">date_histogram</code> 有效。
</dd>
<dt>
<span class="term">
<code class="literal">_term</code>
</span>
</dt>
<dd>
按词项的字符串值的字母顺序排序。只在 <code class="literal">terms</code> 内使用。
</dd>
<dt>
<span class="term">
<code class="literal">_key</code>
</span>
</dt>
<dd>
按每个桶的键值数值排序（理论上与 <code class="literal">_term</code> 类似）。
只在 <code class="literal">histogram</code> 和 <code class="literal">date_histogram</code> 内使用。
</dd>
</dl>
</div>
</div>
<div class="navfooter">
<span class="prev">
<a href="_sorting_multivalue_buckets.html">« 多桶排序</a>
</span>
<span class="next">
<a href="_sorting_by_a_metric.html">按度量排序 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>